<script lang="ts" setup>
import type { EdubaseSchoolClassApi } from '#/api/edu/base/setting/schoolClass';

import { ref } from 'vue';

import { useVbenModal } from '@vben/common-ui';

import { getSchoolClass } from '#/api/edu/base/setting/schoolClass';
import { useDescription } from '#/components/description';

import { useDetailSchema } from '../data';

const formData = ref<EdubaseSchoolClassApi.SchoolClass>();

const [Descriptions] = useDescription({
  bordered: true,
  size: 'middle',
  column: 2,
  labelWidth: 150,
  schema: useDetailSchema(),
});

const [Modal, modalApi] = useVbenModal({
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    let data = modalApi.getData<EdubaseSchoolClassApi.SchoolClass>();
    if (!data || !data.id) {
      return;
    }
    let orgInfoData = {
      showCampus: false,
      showCollege: false,
      showDepartment: false,
      showFaculty: false,
      showGroup: false,
      isSchool: false,
    };
    if (data.isSchool) {
      if (data.showCampus) {
        orgInfoData.campusDictList = data.campusDictList || [];
      }
      if (data.showCollege) {
        orgInfoData.collegeDictList = data.collegeDictList || [];
      }
      if (data.showDepartment) {
        orgInfoData.departmentDictList = data.departmentDictList || [];
      }
      if (data.showFaculty) {
        orgInfoData.facultyDictList = data.facultyDictList || [];
      }
      if (data.showGroup) {
        orgInfoData.groupDictList = data.groupDictList || [];
      }
      orgInfoData.isSchool = data.isSchool;
      orgInfoData.showCampus = data.showCampus;
      orgInfoData.showCollege = data.showCollege;
      orgInfoData.showDepartment = data.showDepartment;
      orgInfoData.showFaculty = data.showFaculty;
      orgInfoData.showGroup = data.showGroup;
      orgInfoData.distabledOrgType = data.distabledOrgType;
    } else {
      orgInfoData = {
        showCampus: false,
        showCollege: false,
        showDepartment: false,
        showFaculty: false,
        showGroup: false,
        isSchool: false,
      };
    }
    modalApi.lock();
    try {
      data = await getSchoolClass(data.id);
      formData.value = { ...data, ...orgInfoData };
    } finally {
      modalApi.unlock();
    }
  },
});
</script>

<template>
  <Modal
    title="查看 班级信息"
    class="w-[60%]"
    :show-cancel-button="false"
    :show-confirm-button="false"
  >
    <Descriptions :data="formData" class="mx-4" />
  </Modal>
</template>
